home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5434 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: news.clark.net!not-for-mail
  2. From: gusty@clark.net (Harlan Messinger)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: External access to private class variables
  5. Date: 2 Feb 1996 22:37:27 GMT
  6. Organization: Clark Internet Services, Inc., Ellicott City, MD USA
  7. Distribution: world
  8. Message-ID: <4eu3n7$rbi@clarknet.clark.net>
  9. References: <4ermr9$ii7@cloner2.ix.netcom.com> <ENNO.96Feb2203423@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  10. NNTP-Posting-Host: explorer.clark.net
  11. Mime-Version: 1.0
  12. Content-Type: TEXT/PLAIN; charset=ISO-8859-1
  13. Content-Transfer-Encoding: 8bit
  14. X-Newsreader: TIN [UNIX 1.3 950726BETA PL0]
  15.  
  16. Enno Sandner (enno@inferenzsysteme.informatik.th-darmstadt.de) wrote:
  17. : In article <4ermr9$ii7@cloner2.ix.netcom.com> genescot@ix.netcom.com(Eugene S. Thompson ) writes:
  18. :    I was playing around with references and ran across this situation. I'm
  19. :    sure it's not original, so I was hoping someone could tell me if it is
  20. :    an intentional implementation of C++ or if it is a bug.
  21. :    class X 
  22. :    {
  23. :    private:
  24. :        int value;
  25. :    public:
  26. :        X (int n = 0) { value = n; }
  27. :        int& GSValue () { return value; }   // return a reference to the 
  28. :                        // private member "value"
  29. :    };
  30. :        .
  31. :        .
  32. :        .
  33. :    main ()
  34. :    {
  35. :    X   x1 (20);
  36. :        printf ("%d\n", x1.GSValue ());     // 20
  37. :    int* pntr = &(x1.GSValue ());
  38. :        printf ("%d\n", *pntr);             // 20
  39. :        *pntr = 30;
  40. :        printf ("%d\n", x1.GSValue ());     // 30 -> We now have external 
  41. :                        // access to a private member.
  42. :    }
  43. :    I realize that this implementation is contrived, but I'm sure there are
  44. :    additional ways of gaining such access. So, what's the deal? 
  45. : It's even possible to modify the private member directly:
  46. : int main ()
  47. : {
  48. :    X x1 (20);
  49. :    printf ("%d\n", x1.GSValue ());     // 20
  50. :    x1.GSValue()=30;
  51. :    printf ("%d\n", x1.GSValue ());     // 30
  52. : }
  53. : You return a non-const reference and therefore it's perfectly valid to
  54. : modify the referred variable. One should take care with services that
  55. : provide uncontrolled access to private members.
  56. :         Enno
  57.  
  58. This isn't strange. An object can have several names and be pointed to by 
  59. several pointers. Making one of those names private hides just that name. 
  60. It doesn't hide any of the other names, and it doesn't hide the pointers 
  61. to it.
  62.  
  63. Treating your example for the sake of discussion as real code that 
  64. someone wrote, all this means is that somebody didn't want the name "x" 
  65. used to access that spot in memory, but wanted it to be called GSValue() 
  66. instead. Perhaps GSValue() is a meaningful name for the intended user, 
  67. and the current implementation of GSValue() is via an integer data 
  68. member, but perhaps later on it will be via some other private mechanism. 
  69. If that happens, the change will be invisible to the user, who will still 
  70. use the public member reference function GSValue() just as before.
  71.